package pers.mydb.ORM;

import pers.mydb.persistence.Column;
import pers.myutil.local.conversion.data.MapConversion;
import pers.myutil.throwable.local.MyUtilLocalException;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对象关系映射
 */
public class ObjectRelationMapping {

    /**
     * 处理结果集 获取map对应记录
     * @param resultSet 结果集
     * @return 数据集
     */
    public static Map<String,Object> handleResultSetToMap(ResultSet resultSet){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            List<String> columnLabels = getColumnLabels(resultSet);
            while (resultSet.next()){
                for (String columnLabel : columnLabels){
                    Object value = resultSet.getObject(columnLabel);
                    map.put(columnLabel,value);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 处理结果集 得到map的list
     * @param resultSet 结果集
     * @return 结果集列表
     */
    public static List<Map<String,Object>> handleResultSetToMapList(ResultSet resultSet){
        // 数据列表 没想好名字
        List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
        try {
            List<String> columnLabels = getColumnLabels(resultSet);
            while (resultSet.next()){
                Map<String,Object> map = new HashMap<String, Object>();
                for(String columnLabel : columnLabels){
                    Object value = resultSet.getObject(columnLabel);
                    map.put(columnLabel,value);
                }
                list.add(map);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取结果集的ColumnCount
     * @param resultSet 结果集
     * @return ColumnLabels
     */
    private static List<String> getColumnLabels(ResultSet resultSet) throws SQLException {
        List<String> labels = new ArrayList<String>();
        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
        for (int i = 0; i < resultSetMetaData.getColumnCount(); i++){
            labels.add(resultSetMetaData.getColumnLabel(i+1));
        }
        return labels;
    }

    /**
     * 渲染到对应的Bean
     * @param clazz 类
     * @param map 数据
     * @param <T> 对象
     * @return 对象
     */
    public static <T> T transfterMapToBeanUnique(Class<T> clazz,Map<String,Object> map) throws NoSuchMethodException, IntrospectionException, IllegalAccessException, InstantiationException, MyUtilLocalException, InvocationTargetException {
        return (T) MapConversion.mapToObject(map,clazz,Column.class);
    }

    public static <T> List<T> transfterMapToBeanList(Class<T> clazz,List<Map<String,Object>> maps) throws NoSuchMethodException, IntrospectionException, IllegalAccessException, InstantiationException, MyUtilLocalException, InvocationTargetException {
        List<T> ts = new ArrayList<T>();
        for(Map<String,Object> map : maps){
            ts.add((T) MapConversion.mapToObject(map,clazz,Column.class));
        }
        return ts;
    }

}
